<template>
  <div class="el-card cardBorder">
    <div class="operate">
      <el-button type="primary" @click="addRelation">添加</el-button>
      <el-input style="width: 30%;margin: 0 5px" v-model="searchParams.relationName" placeholder="请输入名称"></el-input>
      <el-select v-model="searchParams.relationType" placeholder="请选择关系类型" style="margin: 0 5px">
          <el-option value="0" label="亲属关系"></el-option>
        <el-option value="1" label="工作关系"></el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="getAllRelation">搜索</el-button>
    </div>
    <el-table
        :data="relations"
        border
        style="width: 100%;height: 100%;margin-bottom: 10px">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="relationName" label="关系名"></el-table-column>
      <el-table-column prop="userId" label="关系类型">
          <template v-slot="scope">
              <span v-if="scope.row.relationType==='0'">亲属关系</span>
            <span v-if="scope.row.relationType==='1'">工作关系</span>
          </template>
      </el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchParams.pageNum"
        :page-size="searchParams.pageSize"
        :page-sizes="[5, 8, 10, 20]"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
    <relation-dialog :visible.sync="addDialog" title="添加关系" @addResult="getAllRelation"></relation-dialog>
    <relation-update-dialog :visible.sync="updateDialog"  :relation="updateRelation" @addResult="getAllRelation"></relation-update-dialog>
  </div>
</template>

<script>


import {deleteCourse, getCourseByPage} from "@/api/course";
import {getAllUser} from "@/api/user";
import CourseDialog from "@/components/dialog/courseDialog";
import CourseUpdateDialog from "@/components/dialog/courseUpdateDialog";
import {deleteRelation, getRelationByPage} from "@/api/relation";
import RelationDialog from "@/components/dialog/relationDialog";
import RelationUpdateDialog from "@/components/dialog/relationUpdateDialog";

export default {
  name: "index",
  components: {RelationUpdateDialog, RelationDialog, CourseUpdateDialog, CourseDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 5,
        relationName:'',
        relationType:''
      },
      relations:[],
      total:10,
      addDialog:false,
      updateRelation:{},
      updateDialog:false
    }
  },
  methods:{
    getAllRelation(){
      getRelationByPage(this.searchParams).then(res=>{
        if (res.code===200){
          this.$message.success("关系数据获取成功")
          this.relations=res.data.records;
          this.total=res.data.total;
        }
      })
    },
    handleSizeChange(val){
      this.getAllRelation();
    },
    handleCurrentChange(val){
      this.getAllRelation();
    },
    handleEdit(index,row){
      this.updateRelation=JSON.parse(JSON.stringify(row));
      this.updateDialog=true;
    },
    handleDelete(index,row){
      deleteRelation(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功删除")
          this.getAllRelation();
        }
      })
    },
    addRelation(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllRelation();
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
  .operate{
    margin: 16px;
  }
}
</style>
